package cst.neu.yuejian_31.model;

import java.util.List;

import cst.neu.yuejian_31.bean.ClubBean;
import cst.neu.yuejian_31.iface.PopularityIface;
import cst.neu.yuejian_31.iface.PopularityListener;
import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.service.PopularityService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xxe on 2017/10/10.
 */

public class PopularityModel implements PopularityIface {
    private Retrofit retrofit;
//    private String BASEURL="http://10.0.2.2/neu16_yuejian/api/Club/";
    public PopularityModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void getClubList(final PopularityListener popularityListener) {
        PopularityService popularityService=retrofit.create(PopularityService.class);
        Call<List<ClubBean>> call
                =popularityService.getClublist();
        call.enqueue(new Callback<List<ClubBean>>() {
            @Override
            public void onResponse(Call<List<ClubBean>> call, Response<List<ClubBean>> response) {
                if (response.isSuccessful() && response.body() != null)
                {
                    popularityListener.onResponse(response.body());
                }
                else {
                    popularityListener.onFail("FAIL");

                }
            }

            @Override
            public void onFailure(Call<List<ClubBean>> call, Throwable t) {

                popularityListener.onFail(t.toString());
            }
        });
    }
}
